using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Dragoon.RoboBattle
{
  static class Measurements
  {
    public const float RobotLength = 0.015f;
    public const float TerrainMinHeight = 4.9f;
    public const float TerrainMaxHeight = 12.5f;
    public const float TerrainWidth = 32 * 2 * 2;
    public const float TerrainHeight = TerrainWidth;
    public static Vector3 North = Vector3.Backward;
    public static Vector3 South = -North;
    public static Vector3 East = Vector3.Right;
    public static Vector3 West = -East;
    public static Vector3 TerrainNorthEast = Vector3.Up * TerrainMinHeight;
    public static Vector3 TerrainNorthWest = TerrainNorthEast + East * TerrainWidth;
    public static Vector3 TerrainSouthEast = TerrainNorthEast + South * TerrainWidth;
    public static Vector3 TerrainSouthWest = TerrainNorthWest + South * TerrainWidth;
    public static Vector3 TerrainCenter = TerrainNorthEast + TerrainSouthWest * 0.5f;
    public static Vector3 CameraStart = TerrainSouthEast + Vector3.Right * TerrainWidth * 0.5f +
                                        Vector3.Up * TerrainWidth;
    public static IHeightMap HeightMap;
  }

  class GameRoboBattle : GameBase
  {
    public GameRoboBattle()
    {
      Content.RootDirectory = "Content";
    }

    protected override void Initialize()
    {
      const float waterLevel = 5.0f;
      var cam = new HIC.Camera(this);
      var pm = new HeightMapPerlin(this, 32, 32, Measurements.TerrainMinHeight, Measurements.TerrainMaxHeight, 10, 3.7f, 2);
      var hmm = new HeightMapMirror(new HeightMapMirror(pm));
      Measurements.HeightMap = new HeightMapIslandTrim(hmm, 15.0f, waterLevel - 1.0f, 0.5f, IslandShape.Square,
          IslandTrimMethod.Merge);
      var skyNoise = new PerlinNoise(this);
      var skyDome = new SkyDome(this, cam, skyNoise);
      var terrain = new Terrain(this, Measurements.HeightMap, cam,
          new TerrainLevel("dirt", 3, 6),
          new TerrainLevel("rock", 8, 4),
          new TerrainLevel("marsh", 10, 3),
          "rock");
      var sim = new Simulator.RobotSimulator(this, terrain);
      var robren = new HIC.RobotsRenderer(this, sim, cam);
      Components.Add(skyNoise);
      Components.Add(cam);
      Components.Add(skyDome);
      Components.Add(terrain);
      Components.Add(sim);
      Components.Add(robren);
      // Components.Add(new HIC.MeasureFrame(this));
      Components.Add(new FPSCounter(this));
      Components.Add(new Environment(this));
      base.Initialize();
    }


    void graphics_PreparingDeviceSettings(object sender, PreparingDeviceSettingsEventArgs e)
    {
      foreach (Microsoft.Xna.Framework.Graphics.DisplayMode displayMode in GraphicsAdapter.DefaultAdapter.SupportedDisplayModes)
      {
        if (displayMode.Width == 1024 && displayMode.Height == 768)
        {
          e.GraphicsDeviceInformation.PresentationParameters.BackBufferFormat = displayMode.Format;
          e.GraphicsDeviceInformation.PresentationParameters.BackBufferHeight = displayMode.Height;
          e.GraphicsDeviceInformation.PresentationParameters.BackBufferWidth = displayMode.Width;
          e.GraphicsDeviceInformation.PresentationParameters.FullScreenRefreshRateInHz = displayMode.RefreshRate;
          e.GraphicsDeviceInformation.PresentationParameters.IsFullScreen = true;
          return;
        }
      }
    }


    protected override void Update(GameTime gameTime)
    {
      KeyboardState s = Keyboard.GetState();
      if (s.IsKeyDown(Keys.Escape))
        this.Exit();
      base.Update(gameTime);
    }


  }
}


